package com.briup.cms.aop;

import com.briup.cms.bean.BaseLog;
import com.briup.cms.service.IBaseLogService;
import com.briup.cms.utils.ExLogid;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Aspect
@Component
public class LogAspect {

    @Autowired
    private IBaseLogService service;

    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(* com.briup.cms.web.controller.*.*(..)) &&  @annotation(Logging)")
    public void logPointCut(){

    }


    @Around("logPointCut()")
    public Object s(ProceedingJoinPoint pjp){
        Map<String, Object> map = ExLogid.getThreadLocal();

        BaseLog log = new BaseLog();
        log.setLogTime(new Date(System.currentTimeMillis()));
        log.setLogRequestUri((String)map.get("uri"));
        log.setLogRequestMethod((String)map.get("method"));
        log.setLogUsername((String)map.get("username"));
        log.setLogRealname((String)map.get("realname"));

        service.AddLogInfo(log);
        Object obj = null;
        try {
            obj = pjp.proceed();// web 方法
        } catch (Throwable throwable) {
            // throwable.printStackTrace();
            throw  (RuntimeException)throwable;
        }
        return obj;

    }

}
